<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Ramsey\Uuid\Uuid;

class Order extends Model
{

    const TYPE_NORMAL = 'normal';
    const TYPE_CROWDFUNDING = 'crowdfunding';
    const TYPE_SECKILL = 'seckill';

    public static $statusMap=[
        self::TYPE_NORMAL=>'普通订单',
        self::TYPE_CROWDFUNDING=>'众筹订单',
        self::TYPE_SECKILL=>'秒杀订单',
    ];
    protected $fillable = [
        'total_amount', 'remark', 'address', 'closed', 'paid_at',
        'payment_method', 'payment_no', 'ship_status', 'extra',
        'refund_status', 'refund_no', 'reviewed', 'user_coupon_code_id', 'type'
    ];
    protected $casts = [
        'address' => 'json',
        'extra' => 'json'
    ];
    const REFUND_STATUS_PENDING = 'pending';
    const REFUND_STATUS_APPLIED = 'applied';
    const REFUND_STATUS_PROCESSING = 'processing';
    const REFUND_STATUS_SUCCESS = 'success';
    const REFUND_STATUS_FAILED = 'failed';

    const SHIP_STATUS_PENDING = 'pending';
    const SHIP_STATUS_RECEIVED = 'received';
    const SHIP_STATUS_DELIVERED = 'delivered';

    public static $shipMap = [
        self::SHIP_STATUS_RECEIVED => '已收货',
        self::SHIP_STATUS_DELIVERED => '已发货',
        self::SHIP_STATUS_PENDING => '待发货'
    ];
    public static $refundMap = [
        self::REFUND_STATUS_SUCCESS => '退款成功',
        self::REFUND_STATUS_APPLIED => '退款中',
        self::REFUND_STATUS_PROCESSING => '退款处理中',
        self::REFUND_STATUS_FAILED => '退款已拒绝',
        self::REFUND_STATUS_PENDING => '未发起退款',
    ];
    const ALIPAY = 'alipay';
    const WECHATPAY = 'wechat';
    public static $paymentMap = [
        self::ALIPAY => '支付宝',
        self::WECHATPAY => '微信',
    ];

    protected static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        self::creating(function ($model) {
            if (!$model->no) {
                $model->no = static::createOrderNo();
                if (!$model->no) {
                    return false;
                }
            }
        });
    }

    protected static function createOrderNo()
    {
        $prefix = date('YmdHis');
        for ($i = 0; $i < 10; $i++) {
            $no = $prefix . str_pad(random_int(000000, 999999), 6, 0);
            if (!self::query()->where('no', $no)->exists()) {
                return $no;
            }
        }
        return false;
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function item()
    {
        return $this->hasMany(OrderItem::class, 'order_id', 'id');
    }

    public static function createRefundNo()
    {
        do {
            $no = Uuid::uuid4()->getHex();
        } while (self::query()->where('refund_no', $no)->exists());
        return $no;
    }

    public function userCoupon()
    {
        return $this->belongsTo(UserCoupon::class, 'user_coupon_code_id', 'id');
    }

    public function getCrowdfundingStatusAttribute()
    {
        return $this->item[0]->product->crowdfunding->status;
    }
}
